package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import lrq.common.StringUtil;

/**
 * 污水常用字典管理器
 * 
 * @author joey
 *
 */
public class AndroidSewageDomainManager {
	private static AndroidSewageDomainManager sManager;

	private AndroidSewageDomainManager() {
	}

	public static AndroidSewageDomainManager getInstance() {
		if (sManager == null) {
			sManager = new AndroidSewageDomainManager();
		}
		return sManager;
	}

	// 查询站点列表
	public List<RowBase> getStationList(String factoryId) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t.CODE \"id\",\n"
				+ "       t.NAME \"name\",\n"
				+ "       t.flow_type \"flowType\",\n"
				+ "       SUBSTR(t.NAME, INSTR(t.NAME, '水口') - 1) \"flowTypeName\"\n"
				+ "  from BC_SITE_INFO t\n"
				+ " where t.station_category = '0'\n";
		if (!StringUtil.isEmpty(factoryId)) {
			parms.add(new GlobalParameter("factoryId", factoryId));
			sql += "   and t.wwtp_id = @factoryId";
		}

		sql += "  order by t.flow_type desc ";
		return DataUtil.getSelectRows(sql, parms);
	}

	// 查询站点的参数信息
	public List<RowBase> getParameterList(String stationId) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t1.S_DOMAINCODE           \"id\",\n"
				+ "       t1.S_DOMAINCAPTION        \"name\",\n"
				+ "       t.site_id                 \"stationId\",\n"
				+ "       t.DENSITY_ALARM_DOWMLIMIT \"downLimit\",\n"
				+ "       t.DENSITY_ALARM_UPLIMIT   \"upLimit\"\n"
				+ "  from BC_SITE_POLL t\n"
				+ "  left join (select S_DOMAINCODE, S_DOMAINCAPTION\n"
				+ "               from data_defulatdomain\n"
				+ "              where S_DOMAINNAME = 'MONITOR_PARAM') t1\n"
				+ "    on t.name = t1.s_domaincode\n"
				+ " where 1 = 1 ";
		if (!StringUtil.isEmpty(stationId)) {
			parms.add(new GlobalParameter("stationId", stationId));
			sql += " and t.site_id  = @stationId";
		}

		sql += " order by t.site_id, t1.S_DOMAINCODE";
		return DataUtil.getSelectRows(sql, parms);
	}

	// 查询参数信息<站点ID,参数信息>
	public Map<String, List<RowBase>> getParameterMap(String factoryId) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t1.S_DOMAINCODE           \"id\",\n"
				+ "       t1.S_DOMAINCAPTION        \"name\",\n"
				+ "       t.site_id                 \"stationId\",\n"
				+ "       t.DENSITY_ALARM_DOWMLIMIT \"alarmDownLimit\",\n"
				+ "       t.DENSITY_ALARM_UPLIMIT   \"alarmUpLimit\"\n"
				+ "  from BC_SITE_POLL t\n"
				+ "  left join (select S_DOMAINCODE, S_DOMAINCAPTION\n"
				+ "               from data_defulatdomain\n"
				+ "              where S_DOMAINNAME = 'MONITOR_PARAM') t1\n"
				+ "    on t.name = t1.s_domaincode\n"
				+ "  left join BC_SITE_INFO t2\n"
				+ "    on t2.CODE = t.site_id\n"
				+ " where 1 = 1\n";
		if (!StringUtil.isEmpty(factoryId)) {
			parms.add(new GlobalParameter("factoryId", factoryId));
			sql += "   and t2.WWTP_ID = @factoryId\n";
		}
		sql += " order by t.site_id, t1.S_DOMAINCODE";		
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);		
		Map<String, List<RowBase>>resultMap = new HashMap<String, List<RowBase>>();	
		if (rows!=null && rows.size()>0) {
			for (RowBase rowBase : rows) {
				String stationId = rowBase.get("stationId").toString();
				if (!resultMap.containsKey(stationId)) {
					resultMap.put(stationId, new ArrayList<RowBase>());
				}
				resultMap.get(stationId).add(rowBase);
			}
		}		
		return resultMap;
	}

}
